package com.shaoshaohuo.app.ui;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.UMengEventID;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.ui.ec.BuysGoodsActivity;
import com.shaoshaohuo.app.ui.ec.ReleaseOpportunityActivity;
import com.shaoshaohuo.app.ui.ec.SellsGoodsActivity;
import com.shaoshaohuo.app.ui.shipper.ReleaseCarActivity;
import com.shaoshaohuo.app.ui.shipper.SendGoodsActivity;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by cao on 2016/1/18.
 */
public class ReleaseActivity extends BaseActivity implements View.OnClickListener {

    private static final int duration = 1300;
    private static final int offest = 100;
    private static final int closeOffest = 50;
    private static final int closeDuration = 300;


    private LinearLayout ll_release_buy;
    private LinearLayout ll_release_sell;
    private LinearLayout ll_release_opportunity;
    private LinearLayout ll_release_car;
    private LinearLayout ll_release_goods;
    private RelativeLayout rl_close;
    private ImageView iv_close;

    private int screenHeight;
    private int screenWidth;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release);
        initView();
        initLayout();
        initStartAnimation();
        setUpView();
    }

    private void initView() {
        ll_release_buy = (LinearLayout) findViewById(R.id.ll_release_buy);
        ll_release_sell = (LinearLayout) findViewById(R.id.ll_release_sell);
        ll_release_opportunity = (LinearLayout) findViewById(R.id.ll_release_opportunity);
        ll_release_car = (LinearLayout) findViewById(R.id.ll_release_car);
        ll_release_goods = (LinearLayout) findViewById(R.id.ll_release_goods);
        rl_close = (RelativeLayout) findViewById(R.id.rl_close);
        iv_close = (ImageView) findViewById(R.id.iv_close);
    }

    private void initLayout() {
        screenWidth = DeviceHelper.getScreenWidth(this);
        screenHeight = DeviceHelper.getScreenHeight(this);
        setLayout(ll_release_buy);
        setLayout(ll_release_sell);
        setLayout(ll_release_opportunity);
        setLayout(ll_release_car);
        setLayout(ll_release_goods);
    }

    private void setLayout(LinearLayout layout){
        ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
        layoutParams.width = screenWidth/3;
        layout.setLayoutParams(layoutParams);
    }

    private void initStartAnimation() {
        startAnimator(ll_release_buy,0);
        startAnimator(ll_release_sell,1);
        startAnimator(ll_release_opportunity,2);
        startAnimator(ll_release_car,1);
        startAnimator(ll_release_goods,2);
        ObjectAnimator.ofFloat(iv_close, "rotation", 45f, 0f).setDuration(duration).start();
    }

    private void startAnimator(View view,int num){
        view.setVisibility(View.INVISIBLE);
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", screenHeight, 0f).setDuration(duration);
        animator.setInterpolator(new OvershootInterpolator(1f));
        animator.setStartDelay(num * offest);
        animator.addListener(new MyAnimatorListener(view));
        animator.start();

    }

    private void setUpView() {
        ll_release_buy.setOnClickListener(this);
        ll_release_sell.setOnClickListener(this);
        ll_release_opportunity.setOnClickListener(this);
        ll_release_car.setOnClickListener(this);
        ll_release_goods.setOnClickListener(this);
        rl_close.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_release_buy:
                MobclickAgent.onEvent(this, UMengEventID.CLICK_PUBLISH_BUY);
                if (AccountManager.checkUserAuth(this)) {
                    startActivity(new Intent(this, BuysGoodsActivity.class));
                }
                break;
            case R.id.ll_release_sell:
                MobclickAgent.onEvent(this, UMengEventID.CLICK_PUBLISH_SELL);
                if (AccountManager.checkUserAuth(this)) {
                    startActivity(new Intent(this, SellsGoodsActivity.class));
                }
                break;
            case R.id.ll_release_opportunity:
                MobclickAgent.onEvent(this, UMengEventID.CLICK_PUBLISH_BUSINESS);
                if (AccountManager.checkUserAuth(this)) {
                    startActivity(new Intent(this, ReleaseOpportunityActivity.class));
                }
                break;
            case R.id.ll_release_car:
                startActivity(new Intent(this, ReleaseCarActivity.class));
                break;
            case R.id.ll_release_goods:
                startActivity(new Intent(this, SendGoodsActivity.class));
                break;
            case R.id.rl_close:
                close();
                break;
        }
    }

    private void close() {
        endAnimator(ll_release_buy,2);
        endAnimator(ll_release_sell,1);
        endAnimator(ll_release_opportunity,0);
        endAnimator(ll_release_car,2);
        endAnimator(ll_release_goods,1);
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv_close, "rotation", 0f, 45f).setDuration(closeDuration);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
            @Override
            public void onAnimationEnd(Animator animation) {
                ReleaseActivity.this.finish();
            }
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator.start();
    }

    private void endAnimator(View view,int num){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0f, screenHeight).setDuration(closeDuration);
        animator.setStartDelay(num * closeOffest);
        animator.start();
    }

    @Override
    public void onBackPressed() {
        close();
    }

    class MyAnimatorListener implements Animator.AnimatorListener{

        public View view;

        public MyAnimatorListener(View view){
            this.view = view;
        }
        @Override
        public void onAnimationStart(Animator animation) {
            view.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    }
}
